package com.net.xpay.partner.controller.partner;

import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.domain.partner.PartnerCombinationSettleRecord;
import com.net.xpay.common.enums.user.CommissionMode;
import com.net.xpay.common.manager.partner.PartnerCombinationSettleRecordManager;
import com.net.xpay.core.model.PartnerCombinationSettleRecordAggSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.partner.secutiry.RequestUser;
import com.net.xpay.partner.vo.PartnerCombinationSettleRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/partner/partner-combination-settle-record")
@Api(tags = "合伙人/合伙人收益统计", description = "合伙人/合伙人收益统计")
public class PartnerCombinationSettleRecordController {

    @Autowired
    private PartnerCombinationSettleRecordManager partnerCombinationSettleRecordManager;

    @ApiOperation("搜索")
    @PostMapping("/agg-search")
    @ResponseBody
    public ResultPagedList<PartnerCombinationSettleRecordVo> aggSearchPartnerCombinationSettleRecord(@ApiIgnore @RequestUser Partner partner, @RequestBody PartnerCombinationSettleRecordAggSearchModel searchModel) {
        searchModel.setPartnerId(partner.getId());
        List<PartnerCombinationSettleRecord> records = partnerCombinationSettleRecordManager.partnerAggSearch(searchModel);
        long count = partnerCombinationSettleRecordManager.partnerAggSearchCount(searchModel);

        List<PartnerCombinationSettleRecordVo> vos = BeanCopierUtil.batchCopy(PartnerCombinationSettleRecord.class, PartnerCombinationSettleRecordVo.class, records);

        for (PartnerCombinationSettleRecordVo vo : vos) {
            if (vo.getCommissionMode() == CommissionMode.COST_MODE) {
                vo.setPartnerServiceRatio(vo.getServiceRatio().subtract(vo.getCostServiceRatio()));
                vo.setPartnerBankServiceRatio(vo.getBankServiceRatio().subtract(vo.getBankCostServiceRatio()));
            }
        }
        return new ResultPagedList<>(vos, count, searchModel);
    }
}
